<?php
/**
 * 获取学院信息
 * @param int $id 学院id
 * @return array
 */
function get_academy_by_id($id) {
	return M('Academy')->where(array('id' => $id))->find();
}

/**
 * 获取专业信息
 * @param int $id 专业id
 * @return array
 */
function get_major_by_id($id) {
	return M('Major')->where(array('id' => $id))->find();
}

/**
 * 获取当前规则
 * @return array()
 */
function get_current_rules() {
	$role = I('session.role');
	switch ($role) {
		case C('ROLE_SUPERVISOR'):
			return C('RULES_SUPERVISOR');
		case C('ROLE_ADMINISTRATOR'):
			return C('RULES_ADMINISTRATOR');
		case C('ROLE_TEACHER'):
			return C('RULES_TEACHER');
		case C('ROLE_STUDENT'):
			return C('RULES_STUDENT');
	}
}

/**
 * 获取已分类规则
 * @return array()
 */

// array形式
/*
array(3) {
    ["cat0"] => array(3) {
        ["i_class"] => NULL
        ["name"] => NULL
        ["controllers"] => array(1) {
            [0] => array(2) {
                [0] => string(5) "Index"
                [1] => string(0) ""
            }
        }
    }
    ["cat1"] => array(3) {
        ["i_class"] => string(16) "fa fa-university"
        ["name"] => string(12) "学院管理"
        ["controllers"] => array(4) {
            [0] => array(2) {
                [0] => string(7) "Academy"
                [1] => string(12) "学院管理"
            }
            [1] => array(2) {
                [0] => string(5) "Major"
                [1] => string(12) "专业管理"
            }
            [2] => array(2) {
                [0] => string(5) "Class"
                [1] => string(12) "班级管理"
            }
            [3] => array(2) {
                [0] => string(6) "Course"
                [1] => string(12) "课程管理"
            }
        }
    }
    ["cat2"] => array(3) {
        ["i_class"] => string(10) "fa fa-user"
        ["name"] => string(12) "用户管理"
        ["controllers"] => array(3) {
            [0] => array(2) {
                [0] => string(13) "Administrator"
                [1] => string(15) "管理员管理"
            }
            [1] => array(2) {
                [0] => string(7) "Teacher"
                [1] => string(12) "教师管理"
            }
            [2] => array(2) {
                [0] => string(7) "Student"
                [1] => string(12) "学生管理"
            }
        }
    }
}
 */

function get_current_rules_classified() {
	$rules = get_current_rules();
	$category_info = C('CATEGORY_INFO');
	$ret = array();
	$controllers = array();
	foreach ($rules as $key => $rule) {
		if($key != 0 && ($last_cat_id = $rules[$key - 1][0]) != $rule[0]) {
			$ret[$last_cat_id]['i_class'] = $category_info[$last_cat_id][0];
			$ret[$last_cat_id]['name'] = $category_info[$last_cat_id][1];
			$ret[$last_cat_id]['controllers'] = $controllers;
			$controllers = array();
			$temp = $rule;
			array_shift($temp);
			array_push($controllers, $temp);
		} else {
			$temp = $rule;
			array_shift($temp);
			array_push($controllers, $temp);
		}		
	}
	$ret[$rule[0]]['i_class'] = $category_info[$rule[0]][0];
	$ret[$rule[0]]['name'] = $category_info[$rule[0]][1];
	$ret[$rule[0]]['controllers'] = $controllers;
	return $ret;
}

/**
 * 获取规则分类的id
 * @return string
 */
function get_current_category_id() {
	$current_controller_name = CONTROLLER_NAME;
	$current_rules = get_current_rules();
	$category_id = '';
	foreach ($current_rules as $rule) {
		if(in_array($current_controller_name, $rule)) {
			$category_id = $rule[0];
			break;
		}
	}
	return $category_id;
}

/**
 * 获取当前用户的姓名
 * @return string
 */
function get_current_user_name() {
    $role = I('session.role');
    $uid = I('session.uid');
    $name = '';
    switch ($role) {
        case C('ROLE_SUPERVISOR'):
        case C('ROLE_ADMINISTRATOR'):
            $administrator = M('Administrator')->where(array('id' => $uid))->find();
            $name = $administrator['name'];
            break;
        case C('ROLE_TEACHER'):
            $teacher = M('Teacher')->where(array('id' => $uid))->find();
            $name = $teacher['name'];
            break;
        case C('ROLE_STUDENT'):
            $student = M('Student')->where(array('id' => $uid))->find();
            $name = $student['name'];
            break;
    }
    return $name;
}

/**
 * 获取当前角色名称
 * @return string
 */
function get_current_role_name() {
    $role = I('session.role');
    switch ($role) {
        case C('ROLE_SUPERVISOR'):
            $role_name = '超级管理员';
            break;
        case C('ROLE_ADMINISTRATOR'):
            $role_name = '管理员';
            break;
        case C('ROLE_TEACHER'):
            $role_name = '教师';
            break;
        case C('ROLE_STUDENT'):
            $role_name = '学生';
            break;
    }
    return $role_name;
}

/**
 * 获取当前用户id
 * @return string 管理员返回用户名，教师返回工号，学生返回学号
 */
function get_current_user_id() {
    $role = I('session.role');
    $uid = I('session.uid');
    $ret = '';
    switch ($role) {
        case C('ROLE_SUPERVISOR'):
        case C('ROLE_ADMINISTRATOR'):
            $administrator = M('Administrator')->where(array('id' => $uid))->find();
            $ret = $administrator['username'];
            break;
        case C('ROLE_TEACHER'):
            $teacher = M('Teacher')->where(array('id' => $uid))->find();
            $ret = $teacher['id'];
            break;
        case C('ROLE_STUDENT'):
            $student = M('Student')->where(array('id' => $uid))->find();
            $ret = $student['id'];
            break;
    }
    return $ret;
}

/**
 * 获取班级信息
 * @param int $class_id
 * @return array 班级信息
 */
function get_class_by_id($class_id) {
    return M('Class')->where(array('id' => $class_id))->find();
}

/**
 * 查询作业是否提交
 * @param int $homework_id 作业id
 * @param int $student_id 学号
 * @return boolean
 */
/*
function is_homework_submitted($homework_id, $student_id) {
    if(!$student_id) {
        $student_id = I('session.uid');
    }
    $map['homework_id'] = $homework_id;
    $map['student_id'] = $student_id;
    $homework_student = M('homeworkStudent')->where($map)->find();
    if(!$homework_student) {
        return false;
    }
    return true;
}
*/

/**
 * 查询作业是否准时提交
 * @param int $homework_id 作业id
 * @param int $student_id 学号
 * @return boolean
 */
/*
function is_homework_submitted_intime($homework_id, $student_id) {
    if(!$student_id) {
        $student_id = I('session.uid');
    }
    $map['homework_id'] = $homework_id;
    $homework = M('Homework')->where($map)->find();
    $map['student_id'] = $student_id;
    $homework_student = M('homeworkStudent')->where($map)->find();
    if($homework_student['submit_time'] > $homework['end_time']) {
        return false;
    }
    return true;    
}
*/

/**
 * 查询作业是否提交
 * @param int $homework_id 作业id
 * @param int $student_id 学号
 * @return int 0, 1, 2
 * 0. 作业未提交
 * 1. 作业已提交，但超出截止时间
 * 2. 作业已提交，且在规定时间内
 */
function is_homework_submitted($homework_id, $student_id) {
    if(!$student_id) {
        $student_id = I('session.uid');
    }
    $homework = M('Homework')->where(array('id' => $homework_id))->find();
    $map['homework_id'] = $homework_id;
    $map['student_id'] = $student_id;
    $homework_student = M('homeworkStudent')->where($map)->find();
    if(!$homework_student) {
        return 0;
    }
    if($homework_student['submit_time'] > $homework['end_time']) {
        return 1;
    }
    return 2;      
}

function get_homework_submit_time($homework_id, $student_id) {
    if(!$student_id) {
        $student_id = I('session.uid');
    }
    $map['homework_id'] = $homework_id;
    $map['student_id'] = $student_id;
    $homework_student = M('homeworkStudent')->where($map)->find();  
    return $homework_student['submit_time'];
}

/**
 * 查询作业是否已经被评分
 * @param int $homework_id 作业id
 * @param int $student_id 学号
 * @return mixed 未评分则返回false，否则就返回分数
 */
function is_homework_scored($homework_id, $student_id) {
    if(!$student_id) {
        $student_id = I('session.uid');
    }
    $map['homework_id'] = $homework_id;
    $map['student_id'] = $student_id;
    $homework_student = M('homeworkStudent')->where($map)->find();
    if(null === $homework_student['score']) {
        return false;
    }
    return $homework_student['score'];    
}
